package com.bwsoft.admin.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.bwsoft.admin.domain.Standard;
import com.bwsoft.admin.domain.valueObject.StandardVO;

public class StandardAction extends BaseAction_ybd{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2660161232545757329L;
	private static Logger LOG = Logger.getLogger(StandardAction.class);

	public String s_type;  
	public String s_field;
	public String s_uploader;
	public String s_standardName;
	public String s_status;
	public String s_displayStatus;
	
	public String standardIds;
//------------------------------------------------------------	
	public String getS_type() {
		return s_type;
	}

	public void setS_type(String s_type) {
		this.s_type = s_type;
	}

	public String getS_field() {
		return s_field;
	}

	public void setS_field(String s_field) {
		this.s_field = s_field;
	}

	public String getS_uploader() {
		return s_uploader;
	}

	public void setS_uploader(String s_uploader) {
		this.s_uploader = s_uploader;
	}

	public String getS_standardName() {
		return s_standardName;
	}

	public void setS_standardName(String s_standardName) {
		this.s_standardName = s_standardName;
	}

	public String getS_status() {
		return s_status;
	}

	public void setS_status(String s_status) {
		this.s_status = s_status;
	}

	public String getS_displayStatus() {
		return s_displayStatus;
	}

	public void setS_displayStatus(String s_displayStatus) {
		this.s_displayStatus = s_displayStatus;
	}

	public String standardIndex(){
		return "standardIndex";
	}
	
	
	public String getStandardIds() {
		return standardIds;
	}

	public void setStandardIds(String standardIds) {
		this.standardIds = standardIds;
	}

	//--------------------------------------------------------
	/**
	 * 标准查询获取列表
	 */
	public String getStandardJson() {

		int count = 0;
		List<Standard> standardSet=null;
		
			
			//通过字典编号查询字典详情的分页集合
		standardSet = standardService.findData(page, rows, s_type,  s_field, s_uploader, s_standardName, s_status, s_displayStatus);
			//通过编号查询字典集合的大小	
		count = standardService.getTotalCount(s_type,  s_field, s_uploader, s_standardName, s_status, s_displayStatus);
	
		List<StandardVO> standardVOs = standardService.getVo(standardSet);
			
		responseJson.put("total", count);
		responseJson.put("rows", standardVOs);

		return "mapJsonResult";
	}
	
	/**
	 * 保存或新增
	 * @return
	 */
	public String saveOrUpdateStandard(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		if(standard.getType() == null || "".equals(standard.getType())){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "请选择标准类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "请选择标准类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(standard.getField() == null || "".equals(standard.getField())){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "请选择领域！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "请选择领域！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(standard.getStandardId() == null || "".equals(standard.getStandardId())){
			if(annex == null || "".equals(annex)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "请上传附件！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "请上传附件！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
			String fileType = annexFileName.substring(annexFileName.lastIndexOf(".")).toLowerCase();
			if(!".pdf".equals(fileType)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "上传的文件不是PDF类型！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "上传的文件不是PDF类型！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
		}else if(annex != null && !"".equals(annex)){
			String fileType = annexFileName.substring(annexFileName.lastIndexOf(".")).toLowerCase();
			if(!".pdf".equals(fileType)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "上传的文件不是PDF类型！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "上传的文件不是PDF类型！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
			if(!newsService.isFitSize(annex)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "上传的文件不能超过8M！");
//				return "mapJsonResult";
				json.put("result", "Fail");
				json.put("message", "上传的文件不能超过8M！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
		}
		
		msg = standardService.saveOrUpdateStandard(standard, annex, annexFileName);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 批量新增
	 * @return
	 */
	public String saveBatch(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		if(standard.getType() == null || "".equals(standard.getType())){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "请选择标准类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "请选择标准类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(standard.getField() == null || "".equals(standard.getField())){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "请选择领域！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "请选择领域！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		if(annex == null || "".equals(annex)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "请上传附件！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "请上传附件！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		String fileType = annexFileName.substring(annexFileName.lastIndexOf(".")).toLowerCase();
		if(!".zip".equals(fileType)){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "上传的文件不是zip类型！");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "上传的文件不是zip类型！");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		
		msg = standardService.saveBatch(standard, annex, annexFileName);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	/**
	 * 获取详情内容
	 * @return
	 */
	public String standardModifyInfo(){
		standard = standardService.standardModifyInfo(standard.getStandardId());
		
		List<Standard> list = new ArrayList<Standard>();
		list.add(standard);
		List<StandardVO> listVo = standardService.getVo(list);
		
		responseJson.put("standard", listVo.get(0));
		return "mapJsonResult";
	}
	
	/**
	 * 状态改为审核通过
	 * @return
	 */
	public String auditPass(){
		
		msg = standardService.auditPass(standardIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 状态改为审核不通过
	 * @return
	 */
	public String auditNoPass(){
		
		msg = standardService.auditNoPass(standardIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 状态改为显示
	 * @return
	 */
	public String show(){
		
		msg = standardService.show(standardIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 状态改为隐藏
	 * @return
	 */
	public String hide(){
		
		msg = standardService.hide(standardIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	public String remove(){
		
		msg = standardService.remove(standardIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	//2016.08.14新加功能  未登录显示和隐藏
	/**
	 * 未登录时显示状态状态改为显示
	 * @return
	 */
	public String noLoginShow(){
		
		msg = standardService.noLoginShow(standardIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 未登录时显示状态改为隐藏
	 * @return
	 */
	public String noLoginHide(){
		
		msg = standardService.noLoginHide(standardIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	
	
}
